﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;

namespace Accumulation.Controls.Menu {
    /// <summary>
    /// 菜单项
    /// </summary>
    [ParseChildren(true, "Items")
    , PersistChildren(false)
        //, PersistenceMode(PersistenceMode.InnerProperty)
        //, AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)
    , TypeConverter(typeof(ExpandableObjectConverter))]
    public class MenuItemPlus : IStateManager {

        private MenuItemPluses _items;
        private StateBag _viewState;
        private bool _isTrackingViewState;

        public MenuItemPlus() { }

        #region Construction functions

        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        public MenuItemPlus(string text) : this(text, string.Empty, false, null) { }
        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        /// <param name="url">跳转的 url 地址或 javascript 方法名称</param>
        public MenuItemPlus(string text, string url) : this(text, url, false, null) { }
        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        /// <param name="url">跳转的 url 地址或 javascript 方法名称</param>
        /// <param name="enabled">是否只读，会显示，不可操作</param>
        public MenuItemPlus(string text, string url, bool enabled) :
            this(text, url, enabled, null) { }
        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        /// <param name="url">跳转的 url 地址或 javascript 方法名称</param>
        /// <param name="imageUrl">图标 url</param>
        public MenuItemPlus(string text, string url, string imageUrl) :
            this(text, url, false, imageUrl) { }
        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        /// <param name="url">跳转的 url 地址或 javascript 代码</param>
        /// <param name="enabled">是否只读，会显示，不可操作</param>
        /// <param name="imageUrl">图标 url</param>
        public MenuItemPlus(string text, string url, bool enabled, string imageUrl) :
            this(text, url, enabled, imageUrl, TargetEnum._BLANK) { }
        /// <summary>
        /// 菜单项构造函数
        /// </summary>
        /// <param name="text">显示的菜单文字</param>
        /// <param name="url">跳转的 url 地址或 javascript 代码</param>
        /// <param name="enabled">是否只读，会显示，不可操作</param>
        /// <param name="imageUrl">图标 url</param>
        /// <param name="target">菜单 url 打开窗口形式</param>
        public MenuItemPlus(string text, string url, bool enabled, string imageUrl, TargetEnum target) {
            if (!string.IsNullOrEmpty(text)) { this.Text = text; }
            if (!string.IsNullOrEmpty(url)) { this.NavigateUrl = url; }
            if (!string.IsNullOrEmpty(imageUrl)) { this.ImageUrl = imageUrl; }
            this.Target = target;
            this.Enabled = enabled;
        }

        #endregion

        /// <summary>
        /// 菜单的菜单项
        /// </summary>
        [MergableProperty(false)
        , DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
        , Description("菜单的菜单项")
        , PersistenceMode(PersistenceMode.InnerProperty)
        , NotifyParentProperty(true)]
        public MenuItemPluses Items {
            get {
                if (this._items == null) {
                    this._items = new MenuItemPluses();
                }
                if (this._isTrackingViewState) {
                    ((IStateManager)this._items).TrackViewState();
                }
                return this._items;
            }
        }
        /// <summary>
        /// 菜单项父项
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        , Browsable(false)]
        public MenuItemPlus Parent {
            get {
                //if ((this._parent != null) && !this._parent._isRoot) {
                //    return this._parent;
                //}
                return null;
            }
        }
        /// <summary>
        /// 菜单项名称
        /// </summary>
        [Description("菜单项名称")
        , DefaultValue("Name")
        , Localizable(true)
        , NotifyParentProperty(true)]
        public string Name {
            get {
                object obj = this.ViewState["Name"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { this.ViewState["Name"] = value; }
        }
        /// <summary>
        /// 菜单项文字
        /// </summary>
        [Description("菜单项文字")
        , DefaultValue("")
        , Localizable(true)
        , NotifyParentProperty(true)]
        public string Text {
            get {
                object obj = this.ViewState["Text"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { this.ViewState["Text"] = value; }
        }
        /// <summary>
        /// 菜菜单项跳转地址或 JavaScript 方法名称
        /// </summary>
        [Description("菜单项跳转地址")
        , DefaultValue("")
        , NotifyParentProperty(true)]
        public string NavigateUrl {
            get {
                object obj = this.ViewState["NavigateUrl"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { this.ViewState["NavigateUrl"] = value; }
        }
        /// <summary>
        /// JavaScript 代码
        /// </summary>
        [Description("JavaScript 代码")
        , DefaultValue("")
        , NotifyParentProperty(true)]
        public string ClientClick {
            get {
                object obj = this.ViewState["ClientClick"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { this.ViewState["ClientClick"] = value; }
        }
        /// <summary>
        /// 菜单项是否可操作
        /// </summary>
        [Browsable(true)
        , DefaultValue(true)
        , Description("菜单项是否可操作")
        , NotifyParentProperty(true)]
        public bool Enabled {
            get {
                object obj = this.ViewState["Enabled"];
                if (obj != null) { return (bool)obj; }
                return true;
            }
            set { this.ViewState["Enabled"] = value; }
        }
        /// <summary>
        /// 菜单项是否显示
        /// </summary>
        [Browsable(true)
        , DefaultValue(true)
        , Description("菜单项是否显示")
        , NotifyParentProperty(true)]
        public bool Visible {
            get {
                object obj = this.ViewState["Visible"];
                if (obj != null) { return (bool)obj; }
                return true;
            }
            set { this.ViewState["Visible"] = value; }
        }
        /// <summary>
        /// 菜单项图标地址
        /// </summary>
        [Description("菜单项图标地址")
        , UrlProperty
        , DefaultValue("")
        , Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))
        , NotifyParentProperty(true)]
        public string ImageUrl {
            get {
                object obj = this.ViewState["ImageUrl"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { this.ViewState["ImageUrl"] = value; }
        }
        /// <summary>
        /// 菜单 url 打开窗口形式
        /// </summary>
        [Description("菜单 url 打开窗口形式")
        , DefaultValue(TargetEnum._BLANK)
        , NotifyParentProperty(true)]
        public TargetEnum Target {
            get {
                object obj = ViewState["Target"];
                return object.Equals(obj, null) ? TargetEnum._BLANK : (TargetEnum)((int)obj);
            }
            set { ViewState["Target"] = value; }
        }
        /// <summary>
        /// 菜单项类型
        /// </summary>

        #region ViewSate Implemented

        bool IStateManager.IsTrackingViewState {
            get { return this._isTrackingViewState; }
        }
        void IStateManager.LoadViewState(object state) {
            object[] objArray = (object[])state;
            if (objArray != null) {
                if (objArray[0] != null) {
                    ((IStateManager)this.ViewState).LoadViewState(objArray[0]);
                }
                if (objArray[1] != null) {
                    ((IStateManager)this.Items).LoadViewState(objArray[1]);
                }
            }
        }
        object IStateManager.SaveViewState() {
            object[] objArray = new object[2];
            if (this._viewState != null) {
                objArray[0] = ((IStateManager)this._viewState).SaveViewState();
            }
            if (this._items != null) {
                objArray[1] = ((IStateManager)this._items).SaveViewState();
            }
            if ((objArray[0] == null) && (objArray[1] == null)) {
                return null;
            }
            return objArray;
        }
        void IStateManager.TrackViewState() {
            this._isTrackingViewState = true;
            if (this._viewState != null) {
                ((IStateManager)this._viewState).TrackViewState();
            }
            if (this._items != null) {
                ((IStateManager)this._items).TrackViewState();
            }
        }
        private StateBag ViewState {
            get {
                if (this._viewState == null) {
                    this._viewState = new StateBag();
                    if (this._isTrackingViewState) {
                        ((IStateManager)this._viewState).TrackViewState();
                    }
                }
                return this._viewState;
            }
        }

        #endregion

    }
}
